package com.test.nnooolwdeaer.dialog_wooden;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.test.nnooolwdeaer.constant_wooden.Constant_wooden;
import com.test.nnooolwdeaer.util_wooden.StringUtil_wooden;
import com.test.nnooolwdeaer.util_wooden.ThreadUtil_wooden;
import com.test.nnooolwdeaer.util_wooden.http_wooden.HttpManager_wooden;
import com.google.android.material.bottomsheet.BottomSheetDialog;
import com.test.nnooolwdeaer.R;
import com.test.nnooolwdeaer.activity_fragment_wooden.base_wooden.BaseActivity_wooden;
import com.test.nnooolwdeaer.adapter_wooden.WalletAccountTypeListAdapter_Cote_wooden;
import com.test.nnooolwdeaer.bean_wooden.config_info_wooden.AppShowInfo_wooden;
import com.test.nnooolwdeaer.bean_wooden.user_info_wooden.CoteBankAccountList_wooden;
import com.test.nnooolwdeaer.view_wooden.TrackEditText;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class AddAccountDialog2_Cote_wooden extends BottomSheetDialog {

    private BaseActivity_wooden baseActivity_wooden;
    private TrackEditText account1Et_wooden, account2Et_wooden;

    private ImageView accountTypeIv_wooden;
    private TextView accountTypeTv_wooden;
    private ViewGroup accountTypeListLayout_wooden;

    private int itemHeight_wooden =0;

    private String walletChannelStr_wooden;

    private WalletAccountTypeListAdapter_Cote_wooden walletAccountTypeListAdapter_wooden;

    private final CoteBankAccountList_wooden.CoteBankAccount bankAccount_wooden;

    public AddAccountDialog2_Cote_wooden(@NonNull Context context, CoteBankAccountList_wooden.CoteBankAccount bankAccount) {
        super(context,R.style.BottomSheetDialogStyle);
        if (context instanceof BaseActivity_wooden) baseActivity_wooden = (BaseActivity_wooden) context;
        this.bankAccount_wooden = bankAccount;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_add_account_2_cote_wooden);
        getWindow().setBackgroundDrawableResource(R.color.trans);
        getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        setCancelable(false);
        setCanceledOnTouchOutside(false);
        initView_wooden();
    }

    private void initView_wooden(){

        account1Et_wooden = findViewById(R.id.dialog_add_account_2_et_account_1);
        account2Et_wooden = findViewById(R.id.dialog_add_account_2_et_account_2);

        account1Et_wooden.sTrackWord("ADD_ACCOUNT_DIALOG_2_ACCOUNT_1_INPUT");
        account2Et_wooden.sTrackWord("ADD_ACCOUNT_DIALOG_2_ACCOUNT_2_INPUT");

        findViewById(R.id.dialog_add_account_ib_back).setOnClickListener(v -> {
            saveBuriedPoint_wooden(false);
            dismiss();
        });
        findViewById(R.id.dialog_add_account_2_tv_cancel).setOnClickListener(v -> {
            saveBuriedPoint_wooden(false);
            dismiss();
        });

        TextView showAccountTv = findViewById(R.id.dialog_add_account_2_tv_show_account);
        ImageView showAccountIv = findViewById(R.id.dialog_add_account_2_tv_show_account_type);
        if (bankAccount_wooden !=null){
            showAccountTv.setText(bankAccount_wooden.getWalletMobile());
            ThreadUtil_wooden.getInstance_wooden().runOnChildThread_wooden(() -> {
                try {
                    if (TextUtils.isEmpty(bankAccount_wooden.getWalletLogo())) return;
                    URL url = new URL(bankAccount_wooden.getWalletLogo());
                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    connection.setConnectTimeout(5000);
                    connection.setRequestMethod("GET");
                    if (connection.getResponseCode()==200){
                        InputStream inputStream = connection.getInputStream();
                        Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                        ThreadUtil_wooden.getInstance_wooden().runOnUiThread_wooden(() -> {
                            if (showAccountIv!=null) showAccountIv.setImageBitmap(bitmap);
                        });
                    }
                } catch (IOException e) {
                    if (Constant_wooden.Debug) e.printStackTrace();
                }
            });
        }

        //初始化运营商选择组件
        //运营商
        ViewGroup accountTypeLayout = findViewById(R.id.dialog_add_account_2_layout_account_type);
        accountTypeIv_wooden = findViewById(R.id.dialog_add_account_2_iv_account_type);
        accountTypeTv_wooden = findViewById(R.id.dialog_add_account_2_tv_account_type);
        accountTypeListLayout_wooden = findViewById(R.id.dialog_add_account_2_layout_account_type_list);
        ListView accountTypeListLv = findViewById(R.id.dialog_add_account_2_lv_account_type_list);
        accountTypeLayout.setOnClickListener(v -> accountTypeListLayout_wooden.setVisibility(accountTypeListLayout_wooden.getVisibility()==View.VISIBLE ? View.GONE:View.VISIBLE));

        //初始化运营商选项
        accountTypeLayout.setOnClickListener(v -> accountTypeListLayout_wooden.setVisibility(accountTypeListLayout_wooden.getVisibility()==View.VISIBLE ? View.GONE:View.VISIBLE));
        walletAccountTypeListAdapter_wooden = new WalletAccountTypeListAdapter_Cote_wooden(getContext());
        accountTypeListLv.setAdapter(walletAccountTypeListAdapter_wooden);
        accountTypeListLv.setOnItemClickListener((parent, view, position, id) -> {
            accountTypeListLayout_wooden.setVisibility(View.GONE);
            AppShowInfo_wooden.WalletChannel walletChannel = walletAccountTypeListAdapter_wooden.getItem(position);
            walletChannelStr_wooden = walletChannel.getValue();
            accountTypeTv_wooden.setText(walletChannel.getDesc());
            //获取图标
            if (walletChannel.getBitmap()!=null){
                accountTypeIv_wooden.setImageBitmap(walletChannel.getBitmap());
            }else {
                ThreadUtil_wooden.getInstance_wooden().runOnChildThread_wooden(() -> {
                    try {
                        if (TextUtils.isEmpty(walletChannel.getLogo())) return;
                        URL url = new URL(walletChannel.getLogo());
                        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                        connection.setConnectTimeout(5000);
                        connection.setRequestMethod("GET");
                        if (connection.getResponseCode()==200){
                            InputStream inputStream = connection.getInputStream();
                            Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                            walletChannel.setBitmap(bitmap);
                            ThreadUtil_wooden.getInstance_wooden().runOnUiThread_wooden(() -> {
                                if (accountTypeIv_wooden !=null) accountTypeIv_wooden.setImageBitmap(bitmap);
                            });
                        }
                    } catch (IOException e) {
                        if (Constant_wooden.Debug) e.printStackTrace();
                    }
                });
            }
        });

        //设置List高度
        ViewGroup.LayoutParams layoutParams = accountTypeListLv.getLayoutParams();
        if (itemHeight_wooden ==0) itemHeight_wooden =layoutParams.height;
        layoutParams.height = (walletAccountTypeListAdapter_wooden.getCount()>3 ? 3: walletAccountTypeListAdapter_wooden.getCount())* itemHeight_wooden;
        accountTypeListLv.setLayoutParams(layoutParams);

        setCanceledOnTouchOutside(false);

        findViewById(R.id.dialog_add_account_2_btn_submit).setOnClickListener(v -> {
            saveBuriedPoint_wooden(true);
            String account1 = StringUtil_wooden.getSafeString_wooden(account1Et_wooden.gText());
            String account2 = StringUtil_wooden.getSafeString_wooden(account2Et_wooden.gText());
            HttpManager_wooden.getInstance_wooden().bindBankAccount_wooden(account1,account2, walletChannelStr_wooden);
            dismiss();
        });

    }

    //上传埋点
    private void saveBuriedPoint_wooden(boolean isSubmitBtn){
        if (isSubmitBtn)
            HttpManager_wooden.getInstance_wooden().saveUserBuriedPoint("ADD_ACCOUNT_DIALOG_2_SUBMIT_CLICK");
        else
            HttpManager_wooden.getInstance_wooden().saveUserBuriedPoint("ADD_ACCOUNT_DIALOG_2_CANCEL_CLICK");
    }

    @Override
    public void show() {
        super.show();
        HttpManager_wooden.getInstance_wooden().saveUserBuriedPoint("ADD_ACCOUNT_DIALOG_2_COTE_START");
    }

    @Override
    public void dismiss() {
        super.dismiss();
        HttpManager_wooden.getInstance_wooden().saveUserBuriedPoint("ADD_ACCOUNT_DIALOG_2_COTE_END");
    }
}
